<template>
  <div class="login-modal">
    <div class="mask" @click="changeIsShowLoginModal(false)"></div>
    <div class="login-box">
      <i class="el-icon-close" @click="changeIsShowLoginModal(false)"></i>
      <ul class="title">
        <li
          :class="isShowForm == true ? 'active' : ''"
          @click="isShowForm = true"
        >
          手机号码登录
        </li>
        <li style="margin: -2px 10px 0; cursor: auto">|</li>
        <li
          :class="isShowForm == false ? 'active' : ''"
          @click="isShowForm = false"
        >
          微信扫码登录
        </li>
      </ul>
      <div class="body">
        <div class="form" v-show="isShowForm">
          <div class="input-group">
            <input
              type="text"
              placeholder="请输入手机号"
              v-model="phoneNumber"
            />
          </div>
          <div class="input-group">
            <slide-verify
              :l="42"
              :r="20"
              :w="360"
              :h="140"
              :imgs="puzzleList"
              @success="onSuccess"
              @fail="onFail"
              @refresh="onRefresh"
              :style="{ width: '100%' }"
              class="slide-box"
              ref="slideBlock"
              :slider-text="msg"
            ></slide-verify>
          </div>
          <div class="input-group">
            <input type="text" placeholder="请输入验证码" v-model="SMScode" />
            <div class="btn get-code">
              <span v-show="!isShowCount" @click="getSMScode">获取验证码</span>
              <span v-show="isShowCount">{{ count }}s</span>
            </div>
          </div>
          <div class="btn" @click="submitLogin">登录</div>
        </div>
        <div class="qrcode" v-show="!isShowForm">微信二维码</div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapMutations } from "vuex";
import { SendSMSAPI, PhoneLoginAPI } from "@/request/api";
import { validatePhoneNumber } from "@/utils";
export default {
  data() {
    return {
      isShowForm: true,
      msg: "向右滑动",
      phoneNumber: "",
      SMScode: "",
      count: 60,
      maxCount: 60,
      isShowCount: false,
      timer: null,
      puzzleList:[
        require('../assets/img/拼图jlx.png')
      ]
    };
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "ShowLoginModal/changeIsShowLoginModal",
      changeIsLogined: "LoginStatus/changeIsLogined",
    }),
    ...mapActions({
      asyncchangeIsShowToast:"toastStatus/asyncchangeIsShowToast"
    }),
    countDown() {
      this.timer = setInterval(() => {
        this.count--;
        if (this.count == 0) {
          clearInterval(this.timer);
          this.count = this.maxCount;
          this.isShowCount = false;
        }
      }, 1000);
    },
    verifyFn() {
      if (!validatePhoneNumber(this.phoneNumber)) {
        this.asyncchangeIsShowToast({
        msg:"请输入正确手机号!!",
        type:"warning"
      });
        return false;
      }
      if (this.msg == "再试一次" || this.msg == "向右滑动") {
        this.asyncchangeIsShowToast({
        msg:"滑动验证失败，请重新尝试!!",
        type:"warning"
      });
        return false;
      }
      return true;
    },
    async getSMScode() {
      if (!this.verifyFn()) return;
      SendSMSAPI()
        .then((res) => {})
        .catch((err) => {});
      let res = await SendSMSAPI({
        phone: this.phoneNumber,
      });
      if (!res) return;
      this.isShowCount = true;
      this.countDown();
    },
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success,耗时" + ms + "s";
    },
    onFail() {
      this.onRefresh();
    },
    onRefresh() {
      this.msg = "再试一次";
    },
    async submitLogin() {
      if (!this.verifyFn()) return;
      if (!this.SMScode.trim()) {
        this.asyncchangeIsShowToast({
        msg:"请输入验证码!!",
        type:"warning"
      });
        return;
      }
      let res = await PhoneLoginAPI({
        verifyCode: this.SMScode.trim(),
        phone: this.phoneNumber.trim(),
      });
      if (!res) return;
      this.asyncchangeIsShowToast({
        msg:"登录成功!!",
        type:"success"
      });
      this.changeIsShowLoginModal(false);
      localStorage.setItem("x-auth-token", res["x-auth-token"]);
      this.changeIsLogined(true)
      //  console.log(res);
    },
  },
};
</script>

<style lang="less" scoped>
@import "../assets/css/global.less";

.login-modal {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;

  .mask {
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
  }

  .login-box {
    width: 555px;
    height: 423px;
    background: url("../assets/img/登录盒子.png");
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;

    .el-icon-close {
      font-size: 35px;
      position: absolute;
      right: 40px;
      top: 30px;
      cursor: pointer;
    }

    .title {
      display: flex;
      justify-content: center;
      padding-top: 40px;
      font-size: 18px;
      color: grey;

      li {
        cursor: pointer;
      }

      .active {
        color: black;
      }
    }

    .body {
      width: 360px;
      margin: 25px auto 0;

      .form {
        .input-group {
          display: flex;
          justify-content: space-between;
          margin-bottom: 20px;

          input {
            flex: 1;
            height: 48px;
            text-indent: 10px;
            border: 1px solid grey;
          }

          .get-code {
            width: 80px;
            margin-left: 10px;
            padding: 0 10px;
            font-size: 16px;
          }
        }

        .btn {
          background-color: @base-color;
          height: 50px;
          line-height: 50px;
          color: white;
          text-align: center;
          font-size: 18px;
          cursor: pointer;
        }
      }
    }
  }
}

#slideVerify {
  margin-top: -15px;
}

/deep/.slide-verify-slider {
  box-sizing: border-box;
}

/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;

  canvas {
    position: absolute;
    left: 0;
    top: -120px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }

  .slide-verify-block {
    width: 85px;
    height: 136px;
  }

  .slide-verify-refresh-icon {
    top: -120px;
    display: none;
  }

  &:hover {
    canvas {
      display: block;
    }

    .slide-verify-refresh-icon {
      display: block;
    }
  }
}


</style>